﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class NhaSanXuatDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from NhaSanXuat");
        }

        public NhaSanXuat GetById(String mansx)
        {
            NhaSanXuat nsx = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from NhaSanXuat where MaNSX = '" + mansx + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    nsx = new NhaSanXuat();
                    nsx.MaNSX = dataReader.GetString(0);
                    nsx.TenNSX = dataReader.GetString(1);
                    nsx.DiaChi = dataReader.GetString(2);
                    nsx.Sdt = dataReader.GetString(3);
                    nsx.Status = dataReader.GetBoolean(4);
                    return nsx;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return nsx;
        }

        public bool Insert(NhaSanXuat nsx)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[NhaSanXuat]([MaNSX],[TenNSX],[DiaChi],[Sdt],[Status])"
               + "VALUES('" + nsx.MaNSX + "','" + nsx.TenNSX + "','" + nsx.DiaChi + "','" + nsx.Sdt + "'," + nsx.Status + ")";
            return Update(sql);
        }

        public bool DeleteById(String mansx)
        {
            return Update("delete from NhaSanXuat where MaNSX = '" + mansx + "'");
        }

        public bool UpdateById(NhaSanXuat nsx)
        {
            String sql = "update NhaSanXuat "
                 + " set TenNSX = '" + nsx.TenNSX + "',"
                 + " set DiaChi = '" + nsx.DiaChi + "',"
                 + " set Sdt = " + nsx.Sdt + "',"
                 + " set status = " + nsx.Status + "";
            return Update(sql);
        }
    }
}
